package com.cicigodd.brick.plug.pay.service.impl;

import cn.hutool.core.lang.UUID;
import com.cicigodd.brick.plug.common.exception.ServiceException;
import com.cicigodd.brick.plug.common.utils.HttpServletUtil;
import com.cicigodd.brick.plug.pay.config.WeixinPayProperties;
import com.cicigodd.brick.plug.pay.service.WeixinPayService;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * @author cicigodd
 * @date 2022-10-11 11:33
 */
@Slf4j
@Service
public class WeixinPayServiceImpl implements WeixinPayService {


    @Resource
    private WxPayService wxPayService;

    @Resource
    private WeixinPayProperties weixinPayProperties;

    /**
     * 统一下单
     *
     * @param outTradeNo
     * @return
     */
    @Override
    public Map<String,Object> unifiedOrder(String outTradeNo,String code) {

        WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest();
        wxPayUnifiedOrderRequest.setDeviceInfo("WEB");
        wxPayUnifiedOrderRequest.setBody("生鲜食品零食");
        wxPayUnifiedOrderRequest.setOutTradeNo(UUID.fastUUID().toString().replaceAll("-",""));
        wxPayUnifiedOrderRequest.setTotalFee(100000);
        wxPayUnifiedOrderRequest.setFeeType("CNY");
        wxPayUnifiedOrderRequest.setSpbillCreateIp(HttpServletUtil.getRequestClientIp(HttpServletUtil.getRequest()));
        wxPayUnifiedOrderRequest.setNotifyUrl(weixinPayProperties.getPayNotifyUrl());
        wxPayUnifiedOrderRequest.setTradeType("JSAPI");
        wxPayUnifiedOrderRequest.setOpenid("openId");

        Object order;
        try {
            order = wxPayService.createOrder(wxPayUnifiedOrderRequest);
        } catch (WxPayException e) {
            throw new ServiceException("创建订单失败,请稍后重试");
        }
        WxPayUnifiedOrderResult wxPayUnifiedOrderResult = null;
        try {
            wxPayUnifiedOrderResult = wxPayService.unifiedOrder(wxPayUnifiedOrderRequest);
        } catch (WxPayException e) {
            throw new ServiceException("下单失败,请稍后重试");
        }
        Map<String,Object> result = new HashMap<>();
        result.put("appid",wxPayUnifiedOrderResult.getAppid());
        result.put("nonceStr",wxPayUnifiedOrderResult.getNonceStr());
        result.put("prepayId",wxPayUnifiedOrderResult.getPrepayId());
        result.put("sign",wxPayUnifiedOrderResult.getAppid());
        result.put("timestamp",wxPayUnifiedOrderRequest.getNonceStr());
        result.put("order",order);

        return result;
    }

    @Override
    public Object payNotify(String body) {
        WxPayOrderNotifyResult result = null;
        try {
            result = wxPayService.parseOrderNotifyResult(body);
            if ("SUCCESS_CODE".equals(result.getReturnCode())) {
                //返回微信接收成功，否则微信会一直调用此接口
                WxPayNotifyResponse response = new WxPayNotifyResponse();
                response.setReturnCode("SUCCESS");
                response.setReturnMsg("Ok");
                return response;
            }
            return null;
        } catch (WxPayException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询订单
     *
     * @param outTradeNo 订单号
     * @return
     */
    @Override
    public WxPayOrderQueryResult queryOrder(String outTradeNo) {
        WxPayOrderQueryResult wxPayOrderQueryResult = null;
        try {
            wxPayOrderQueryResult = wxPayService.queryOrder(null, outTradeNo);
        } catch (WxPayException e) {
            throw new ServiceException("查询订单失败");
        }
        return wxPayOrderQueryResult;
    }

    /**
     * 申请退款
     *
     * @param id
     */
    @Override
    public void refund(String id) {
        //组装退款对象
        WxPayRefundRequest request = new WxPayRefundRequest();
        request.setOutTradeNo("");
        request.setOutRefundNo("");
        request.setTotalFee(0);
        request.setRefundFee(0);
        request.setNotifyUrl(weixinPayProperties.getRefundNotifyUrl());
        try {
            WxPayRefundResult wxPayRefundResult = wxPayService.refund(request);
        } catch (WxPayException e) {
            throw new ServiceException("申请退款失败");
        }
    }

    /**
     * 申请退款回调
     *
     * @param body
     */
    @Override
    public Object refundNotify(String body) {
        WxPayRefundNotifyResult wxPayRefundNotifyResult = null;
        try {
            wxPayRefundNotifyResult = wxPayService.parseRefundNotifyResult(body);
        } catch (WxPayException e) {
            e.printStackTrace();
        }
        WxPayRefundNotifyResult.ReqInfo reqInfo = wxPayRefundNotifyResult.getReqInfo();
        if ("SUCCESS_CODE".equals(wxPayRefundNotifyResult.getReturnCode())) {
            //返回微信接收成功，否则微信会一直调用此接口
            WxPayNotifyResponse response = new WxPayNotifyResponse();
            response.setReturnCode("SUCCESS");
            response.setReturnMsg("Ok");
            return response;
        }
        return null;
    }
}
